<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改员工</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../js/layui/css/layui.css" media="all">
    <style>
        .layui-form-label.layui-required:after{
            content:"*";
            color:red;
            position: absolute;
            top:5px;
            left:30px;
        }
    </style>
</head>
<body>
<div style="width:80%;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="" id="update_staff">
        <input type="hidden" class="input-text" id="id" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-label layui-required">员工编号</label>
            <div class="layui-input-block">
                <input type="text" name="bh" required disabled lay-verify="required" placeholder="请输入员工编号" autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">员工姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">昵称</label>
            <div class="layui-input-block">
                <input type="text" name="nicheng" placeholder="请输入昵称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block" id="xinbie">
                <input type="radio" name="sex" value="1" id="nan" title="男" checked=""><div id="y" class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i>男</div>
                <input type="radio" name="sex" value="0" id="nv" title="女"><div id="x" class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>女</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-block">
                <input type="text" name="phone" placeholder="请输入联系方式" autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">家庭住址</label>
            <div class="layui-input-block">
                <input type="text" name="address"  placeholder="请输入地址" autocomplete="off" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">国籍</label>
            <div class="layui-input-block">
                <select lay-search id="guoji">

                </select>
            </div>
        </div>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">职业</label>-->
<!--            <div class="layui-input-block">-->
<!--                <select lay-verify="required" lay-search id="zhiye">-->

<!--                </select>-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">证件号码</label>
            <div class="layui-input-block">
                <input type="text" name="sfz" placeholder="请输入证件号码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属企业</label>
            <div class="layui-input-block">
                <select lay-verify="required" lay-search id="cla">

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">出生日期</label>
            <div class="layui-input-inline">
                <input type="text" name="cstime" id="cstime" lay-verify="required" autocomplete="off" placeholder="请输入出生日期" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">入职时间</label>
            <div class="layui-input-inline">
                <input type="text" name="rztime" id="start_time" autocomplete="off" placeholder="请输入入职时间" class="layui-input">
            </div>
            <!--<div class="layui-form-mid layui-word-aux">辅助文字</div>-->
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">到期时间</label>
            <div class="layui-input-inline">
                <input type="text" name="dqtime" id="end_time" autocomplete="off" placeholder="请输入到期时间" class="layui-input">
            </div>
        </div>
        <!--<div class="layui-form-item" style="display: none;" id="hahahha">-->
            <!--<label class="layui-form-label">离职时间</label>-->
            <!--<div class="layui-input-inline">-->
                <!--<input type="text" name="lztime" id="end_time" lay-verify="required" autocomplete="off" placeholder="请输入离职时间" class="layui-input">-->
            <!--</div>-->
            <!--&lt;!&ndash;<div class="layui-form-mid layui-word-aux">辅助文字</div>&ndash;&gt;-->
        <!--</div>-->
        <!--<div class="layui-form-item">-->
            <!--<label class="layui-form-label">状态</label>-->
            <!--<div class="layui-input-block" id="status">-->
                <!--<input type="radio" name="status" value="1" id="zai" title="在职" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i>在职</div>-->
                <!--<input type="radio" name="status" value="0" id="li" title="离职"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>离职</div>-->
            <!--</div>-->
        <!--</div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea class="layui-textarea" type="text" id="message" name="message" placeholder="请输入备注" autocomplete="off" class="layui-input"></textarea>
            </div>
        </div>
        <!--<div class="layui-form-item">-->
            <!--<label class="layui-form-label">缴费状态</label>-->
            <!--<div class="layui-input-block">-->
                <!--<input type="radio" name="jfstatus" value="2" id="c" title="已到期" checked=""><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i>已到期</div>-->
                <!--<input type="radio" name="jfstatus" value="1" id="b" title="快到期"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>快到期</div>-->
                <!--<input type="radio" name="jfstatus" value="0" id="a" title="正常"><div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"></i>正常</div>-->
            <!--</div>-->
        <!--</div>-->
        <!-- 图片上传 -->
        <div class="layui-upload">
            <button type="button" class="layui-btn" id="test" style="display: inline;float: left;">上传头像</button>
            <div class="layui-upload-list" style="display: inline;margin-left: 30px;">
                <img class="layui-upload-img" name="picture" id="picture" style="width: 200px;height: 150px">
                <p id="demoText"></p>
            </div>
            <input type="hidden" class="input-text" id="logo" name="logo">
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addWuye" lay-submit lay-filter="addWuye">立即修改</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="../../js/layui/layui.js"></script>
<script src="../../js/layui/layui.all.js"></script>
<script src="../../js/token.js"></script>
<script src="../../js/config.js"></script>

<script>

    //url 传参
    function GetQueryString(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var a = decodeURI(window.location.search);
        var r = a.substr(1).match(reg);//search,查询？后面的参数，并匹配正则
        if(r!=null)return  unescape(r[2]); return null;
    }

    window.onload = function(){
        var form = layui.form;
        $("input[name='id']").val(GetQueryString("id"));
        $("input[name='name']").val(decodeURIComponent(GetQueryString("name")));
        // var nicheng = GetQueryString("nicheng");
        // if(nicheng==null || nicheng==""){
        //     $("input[name='nicheng']").val("");
        // }else{
        //     $("input[name='nicheng']").val(nicheng);
        // }

        $("input[name='bh']").val(GetQueryString("bh"));
        var phone = decodeURIComponent(GetQueryString("phone"));
        if (phone=="null" || phone==null || phone=="undefined" || phone == "") {
            $("input[name='phone']").val("");
        }else {
            $("input[name='phone']").val(phone);
        }
        var address = decodeURIComponent(GetQueryString("address"));
        if (address=="null" || address==null || address=="undefined" || address == "") {
            $("input[name='address']").val("");
        }else {
            $("input[name='address']").val(decodeURIComponent(GetQueryString("address")));
        }
        $("input[name='logo']").val(GetQueryString("picture"));

        var sfz = decodeURIComponent(GetQueryString("sfz"));
        if (sfz=="null" || sfz==null || sfz=="undefined" || sfz == "") {
            $("input[name='sfz']").val("");
        }else {
            $("input[name='sfz']").val(sfz);
        }
        var cstime = GetQueryString("cstime");
        if (cstime.substr(0,2) != "00") {
            // $("#cstime").attr("disabled", "disabled");
            $("input[name='cstime']").val(cstime);
        }else {
            $("input[name='cstime']").val(null);
        }

        var nicheng = decodeURIComponent(GetQueryString("nicheng"));
        if (nicheng=="null" || nicheng==null || nicheng=="undefined" || nicheng == "") {
            $("input[name='nicheng']").val("");
        }else {
            $("input[name='nicheng']").val(nicheng);
        }
        var rztime = GetQueryString("rztime");
        if (rztime=="null" || rztime==null || rztime=="undefined" || rztime == "") {
            $("input[name='rztime']").val("");
        }else {
            $("input[name='rztime']").val(GetQueryString("rztime"));
        }
        var dqtime = GetQueryString("dqtime");
        if (dqtime=="null" || dqtime==null || dqtime=="undefined" || dqtime == "") {
            $("input[name='dqtime']").val("");
        }else {
            $("input[name='dqtime']").val(GetQueryString("dqtime"));
        }
        // var lizhiTime = GetQueryString("lztime");
        // console.log("离职时间："+lizhiTime);
        // if(lizhiTime!=null || lizhiTime!==""){
        //     $("input[name='lztime']").val(lizhiTime);
        //     $(document).find("#hahahha").css("display" , "block");
        // }
        // $("input[name='zhiye']").val(GetQueryString("zhiye"));
        $("#message").val(decodeURIComponent(GetQueryString("message")));
        $('#picture').attr('src', HOST+"upload/"+GetQueryString("picture"));
        var x = GetQueryString("sex");
        if(x==1){
            $("#nan").prop("checked",true);
            form.render()
        }else{
            $("#nv").prop("checked",true);
            form.render()
        }
    }

    layui.use(['form' , 'jquery' , 'upload'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;

        //自定义验证规则  检测是否为身份证号
        verify: {
            required:
                identity: [/(^\d{18}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
        }

        //修改操作
        form.on('submit(addWuye)',function(data){
            data.field.picture=$("input[name='logo']").val();
            data.field.qyID = $("#cla").find("option:selected").val();
            var guoji =$("#guoji").find("option:selected").text();
            // var zhiye =$("#zhiye").find("option:selected").text();
            data.field.guoji = guoji;
            // data.field.zhiye = zhiye;
            data.field.id = $("#id").val();
            var cstime = $("#cstime").val();
            if (cstime.substr(0,2) == "00") {
                layer.msg('出生日期无效，请重新选择');
            }else{
                $.ajax({
                    url:HOST+"staff/updatestaff"
                    ,type:"POST"
                    ,contentType: 'application/json'
                    ,data:JSON.stringify(data.field)
                    ,success:function (data) {
                        if(data.code==3){
                            layer.msg(data.msg);
                        }else if(data.code==2){
                            layer.msg('该名字已存在，请重新输入！');
                        }else if(data.code==5) {
                            layer.msg(data.msg);
                        }else if(data.code==0){
                            layer.close(layer.index);
                            layer.msg(data.msg);
                            setTimeout(function(){
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);//关闭弹出层
                                parent.location.reload();//重新加载父页面表格
                            }, 2000);
                        }else if(data.code==1){
                            layer.msg(data.msg);
                        }else{
                            layer.msg('拒绝访问');
                        }
                    }
                    ,error:function () {
                        console.log("ajax error");
                    }
                });
            }
            return false;
        });

        //遍历企业下选框
        $.ajax({
            url: HOST+"qiye/selectqiyestatus"
            , type: "POST"
            , contentType: 'application/json'
            , data: {'status': 1}
            , success: function (data) {
                // if (decodeURIComponent(GetQueryString("qyName")) != null && decodeURIComponent(GetQueryString("qyName")) != "" && decodeURIComponent(GetQueryString("qyName")) != undefined) {
                //     $('#cla').append(new Option(decodeURIComponent(GetQueryString("qyName")),GetQueryString("qyID"),true));
                // }
                $.each(data.data, function (index, item) {
                    $('#cla').append(new Option(item.qyname, item.id));// 下拉菜单里添加元素
                });
                // 当前的select的id
                $('#cla').val(GetQueryString("qyID"));
                layui.form.render("select");
            }
        });

        //遍历国籍下选框
        $.ajax({
            url: HOST+"country/selectCountry"
            , type: "POST"
            , contentType: 'application/json'
            , success: function (data) {
                $('#guoji').append(new Option(decodeURIComponent(GetQueryString("guoji")),"0",true));
                $.each(data.data, function (index, item) {
                    $('#guoji').append(new Option(item.zhName, item.id));// 下拉菜单里添加元素
                });
                layui.form.render("select");
            }
        });

        //遍历职业下选框
        // $.ajax({
        //     url: HOST+"staff/getZyDic?type=1"
        //     , type: "POST"
        //     , contentType: 'application/json'
        //     , success: function (data) {
        //         $('#zhiye').append(new Option(GetQueryString("zhiye"),"-1",true));
        //         $.each(data.data, function (index, item) {
        //             $('#zhiye').append(new Option(item.name, item.code));// 下拉菜单里添加元素
        //         });
        //         layui.form.render("select");
        //     }
        // });

        //logo上传
        var uploadInst = upload.render({
            elem: '#test'
            , url: HOST+'upload' //改成您自己的上传接口
            , before: function (obj) {
                layui.layer.load();
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#picture').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //判断上传是否成功
                if (res.code == 0) {
                    $("#logo").val(res.data);
                    var index2 = layui.layer.load();
                    layer.close(index2);
                    return layer.msg('上传成功！');
                }else{
                    var index = layui.layer.load();
                    layer.close(index);
                    return layer.msg('上传失败！');
                }
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #ff5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });
</script>
<script>
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        var endDate= laydate.render({
            elem: '#end_time',//选择器结束时间
            type: 'datetime',
            min:"1970-1-1",//设置min默认最小值
            done: function(value,date){
                startDate.config.max={
                    year:date.year,
                    month:date.month-1,//关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds : 0
                }
            }
        });

        //日期范围
        var startDate=laydate.render({
            elem: '#start_time',
            type: 'datetime',
            max:"2099-12-31",//设置一个默认最大值
            done: function(value, date){
                endDate.config.min ={
                    year:date.year,
                    month:date.month-1, //关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds : 0
                };
            }
        });

        laydate.render({
            elem: '#cstime',
            type: 'date',
            format: 'yyyy/MM/dd'
        });
    });
</script>
</body>
</html>